package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderManage;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    OrderService orderService;

    /**
     *分页查询
     * @return
     */
    @RequestMapping("/findOrdersPage")
    public PageResult findOrdersPage(@RequestBody Map map){

        PageInfo<OrderManage> pageInfo = orderService.findPage(map);

        long total = pageInfo.getTotal();
        List<OrderManage> list = pageInfo.getList();
        return new PageResult(total, list);
    }

   //提交新增数据
    @RequestMapping("/submit")
//    public Result submit(@RequestBody OrderManage orderManage, Integer[] setmealId) throws Exception {
//
//        HashMap<String, Object> map = new HashMap<>();
//        if (setmealId.length > 0) {
//            Integer id = setmealId[0];
//            map.put("setmealId", id);
//        }
//        Integer id = orderManage.getId();
//        Integer memberId = orderManage.getMemberId();
//        String memberName = orderManage.getMemberName();
//        Date orderDate = orderManage.getOrderDate();
//        String date = DateUtils.parseDate2String(orderDate);
//        String orderStatus = orderManage.getOrderStatus();
//        String orderType = orderManage.getOrderType();
//        String phoneNumber = orderManage.getPhoneNumber();
//        Integer setmealId1 = orderManage.getSetmealId();
//        String setmealName = orderManage.getSetmealName();
//
//        map.put("id", id);
//        map.put("memberId", memberId);
//        map.put("memberName", memberName);
//        map.put("setmealId", setmealId1);
//        map.put("setmealName", setmealName);
//        map.put("telephone", phoneNumber);
//        map.put("orderStatus", orderStatus);
//        map.put("orderType", orderType);
//        map.put("orderDate", date);
//
//        try {
//            orderService.add(map);
//            return new Result(true, MessageConstant.ORDER_SUCCESS);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, "预约失败");
//        }
//
//
//////删除
////    @RequestMapping("/delete")
////    public Result delete(Integer id){
////        try {
////            orderService.delete(id);
////            return new Result(true,"删除预约成功");
////        } catch (Exception e) {
////            e.printStackTrace();
////            return new Result(false,"删除预约失败");
////        }
////    }
////
//
//    }
    public Result add(@RequestBody OrderManage orderManage, Integer[] setmealId) throws Exception {

        HashMap<String, Object> map = new HashMap<>();
        if (setmealId.length > 0) {
            Integer id = setmealId[0];
            map.put("setmealId", id);
        }

        String phoneNumber = orderManage.getPhoneNumber();
        String orderStatus = orderManage.getOrderStatus();
        String orderType = orderManage.getOrderType();
        Date orderDate = orderManage.getOrderDate();
        String date = DateUtils.parseDate2String(orderDate);
        String name = orderManage.getMemberName();


        map.put("name", name);
        map.put("telephone", phoneNumber);
        map.put("orderStatus", orderStatus);
        map.put("orderType", orderType);
        map.put("orderDate", date);

        try {
            orderService.add(map);
            return new Result(true, MessageConstant.ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "预约失败");
        }

    }


















//编辑页面查询
    @RequestMapping("/findById")
    public Result findById(int id){
        try {
            Map map = orderService.findOrderById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @RequestMapping("/findSetmealIdByOrderId")
    public Result findSetmealIdByOrderId(int id){
        try {
         List<Setmeal>list= orderService.findSetmealIdByOrderId(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }


   //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Map map, Integer setmealId){
        try {
            orderService.edit(map,setmealId);
        }catch (Exception e){
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            orderService.delete(id);
            return new Result(true,"删除预约成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除预约失败");
        }
    }



    //一键修改
    @RequestMapping("/changeStatus")
    public Result changeStatus(@RequestBody Map map){
        try {
            orderService.changeStatus(map);
            return new Result(true,"修改状态成功");

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改状态失败");
        }
    }

}
